/**
 *
 */
package com.zhuoyue.elmt.service;

import java.util.HashMap;
import java.util.List;

import com.eit.eitsite.common.security.Digests;
import com.eit.eitsite.common.utils.Encodes;
import com.eit.eitsite.common.utils.StringUtils;
import com.eit.eitsite.modules.sys.dao.RoleDao;
import com.eit.eitsite.modules.sys.dao.UserDao;
import com.eit.eitsite.modules.sys.entity.Office;
import com.eit.eitsite.modules.sys.entity.Role;
import com.eit.eitsite.modules.sys.entity.User;
import com.eit.eitsite.modules.sys.service.SystemService;
import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.zhuoyue.elmt.entity.MaintenanceStaffDevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.service.CrudService;
import com.zhuoyue.elmt.entity.MaintenanceStaff;
import com.zhuoyue.elmt.dao.MaintenanceStaffDao;

/**
 * 维保人员备案Service
 *
 * @author chenhao
 * @version 2016-09-08
 */
@Service
@Transactional(readOnly = true)
public class MaintenanceStaffService extends CrudService<MaintenanceStaffDao, MaintenanceStaff> {

    @Autowired
    private SystemService systemService;

    public MaintenanceStaff get(String id) {
        return super.get(id);
    }

    public List<MaintenanceStaff> findList(MaintenanceStaff maintenanceStaff) {
        return super.findList(maintenanceStaff);
    }

    public List<MaintenanceStaff> findDeviceMainStaffList(MaintenanceStaff maintenanceStaff) {
        return dao.findDeviceMainStaffList(maintenanceStaff);
    }

    public List<MaintenanceStaff> findPushDeviceMainStaffList(MaintenanceStaff maintenanceStaff) {
        return dao.findPushDeviceMainStaffList(maintenanceStaff);
    }

    public Page<MaintenanceStaff> findPage(Page<MaintenanceStaff> page, MaintenanceStaff maintenanceStaff) {
        return super.findPage(page, maintenanceStaff);
    }

    public Page<MaintenanceStaff> staffgrouppage(Page<MaintenanceStaff> page, MaintenanceStaff maintenanceStaff) {
        maintenanceStaff.setPage(page);
        page.setList(dao.staffgrouplist(maintenanceStaff));
        return page;
    }

    public List<MaintenanceStaff> staffgrouplist(MaintenanceStaff maintenanceStaff) {
        return dao.staffgrouplist(maintenanceStaff);
    }

    public List<MaintenanceStaff> staffgroupnolist(HashMap qryMap) {
        return dao.staffgroupnolist(qryMap);
    }

    public List<HashMap> findStaffDeviceList(MaintenanceStaffDevice maintenanceStaffDevice) {
        return dao.findStaffDeviceList(maintenanceStaffDevice);
    }

    @Transactional(readOnly = false)
    public void save(MaintenanceStaff maintenanceStaff) {
        save(maintenanceStaff, true);
    }

    @Transactional(readOnly = false)
    public void save(MaintenanceStaff maintenanceStaff, boolean relaUser) {
        super.save(maintenanceStaff);

        if (relaUser) {
            // 同步到系统用户管理 增加或更新
            // User user = systemService.getUserByLoginName(maintenanceStaff.getLoginName(), maintenanceStaff.getMaintenanceUnitId());
            User user = systemService.getUserByLoginName(maintenanceStaff.getLoginName());
            if (user == null) {
                user = new User();
            }
            user.setName(maintenanceStaff.getStaffName());
            // 登录名=机构代码+员工号
            user.setLoginName(maintenanceStaff.getLoginName());
            user.setNo(maintenanceStaff.getLoginName());
            user.setPassword(maintenanceStaff.getPasswd());
            user.setPhone(maintenanceStaff.getContactPhone());
            user.setMobile(maintenanceStaff.getMobileNumber());
            user.setEmail(maintenanceStaff.getEmail());
            user.setCompany(new Office(UserUtils.getUser().getCompany().getId()));
            //user.setOffice(new Office(UserUtils.getUser().getOffice().getId()));
            user.setPhoto(maintenanceStaff.getStaffPhoto());
            user.setWorkType(maintenanceStaff.getWorkType());
            Office _office=new Office();
            _office.setId(maintenanceStaff.getOfficeId());
            _office.setName(maintenanceStaff.getOfficeName());
            _office.setType(maintenanceStaff.getOfficeType());
            user.setOffice(_office);
            // 用户角色
            //List<Role> roleList = Lists.newArrayList();
            //Role role = new Role();
            //role.setId(maintenanceStaff.getUserRole());
            // 维保人员-3 管理人员-2
            //user.setUserType(maintenanceStaff.getUserRole());
            //roleList.add(role);
            //user.setRoleList(roleList);
            systemService.saveUser(user, false);
        }
    }

    @Transactional(readOnly = false)
    public void delete(MaintenanceStaff maintenanceStaff) {
        super.delete(maintenanceStaff);

        // 同步到系统用户管理 增加或更新
        // User user = systemService.getUserByLoginName(maintenanceStaff.getLoginName(), maintenanceStaff.getMaintenanceUnitId());
        User user = systemService.getUserByLoginName(maintenanceStaff.getLoginName());
        if (user != null) {
            systemService.deleteUser(user);
        }
    }

    public MaintenanceStaff getByLoginName(String loginName) {
        return dao.getByLoginName(loginName);
    }

    public MaintenanceStaff getByLoginNameAndOrgcode(MaintenanceStaff qryMaintenanceStaff) {
        return dao.getByLoginNameAndOrgcode(qryMaintenanceStaff);
    }


    //	@Transactional(readOnly = false)
    //	public void save(MaintenanceStaff maintenanceStaff) {
    //		if (maintenanceStaff.getIsNewRecord()){
    //			dao.insert(maintenanceStaff);
    //		}else{;
    //			dao.update(maintenanceStaff);
    //		}
    //	}
}